import { parse } from 'url';
// mock tableListDataSource
let tableListDataSource = [
  {
    key: 1,
    shzt: '待审核',
    
    zfzt: '未支付',
    slr: '徐凤年',
    dlrs: '2',
    slje: 2000,
    lxdh: '13900890012',
    sqsj: '2019-12-12: 12:20',
    hbh: 'SC8999',
    hbrq: '2019-12-09',
    lqfs: '微信',
    lqsj: '2019-12-09',
    zh: '333333333',
    khm: '徐凤年',
    khyh: '建设银行'
  },
  {
    key: 2,
    shzt: '已审核',
    zfzt: '支付成功',
    slr: '徐凤年',
    dlrs: '2',
    slje: 2000,
    lxdh: '13900890012',
    sqsj: '2019-12-12: 12:20',
    hbh: 'SC8999',
    hbrq: '2019-12-09',
    lqfs: '微信',
    lqsj: '2019-12-09',
    zh: '333333333',
    khm: '徐凤年',
    khyh: '建设银行'
  },
  {
    key: 3,
    shzt: '已审核',
    zfzt: '支付成功',
    slr: '徐凤年',
    dlrs: '2',
    slje: 2000,
    lxdh: '13900890012',
    sqsj: '2019-12-12: 12:20',
    hbh: 'SC8999',
    hbrq: '2019-12-09',
    lqfs: '微信',
    lqsj: '2019-12-09',
    zh: '333333333',
    khm: '徐凤年',
    khyh: '建设银行'
  },
  {
    key: 4,
    shzt: '已审核',
    zfzt: '支付成功',
    slr: '徐凤年',
    dlrs: '2',
    slje: 2000,
    lxdh: '13900890012',
    sqsj: '2019-12-12: 12:20',
    hbh: 'SC8999',
    hbrq: '2019-12-09',
    lqfs: '微信',
    lqsj: '2019-12-09',
    zh: '333333333',
    khm: '徐凤年',
    khyh: '建设银行'
  },
  {
    key: 5,
    shzt: '已审核',
    zfzt: '支付成功',
    slr: '徐凤年',
    dlrs: '2',
    slje: 2000,
    lxdh: '13900890012',
    sqsj: '2019-12-12: 12:20',
    hbh: 'SC8999',
    hbrq: '2019-12-09',
    lqfs: '微信',
    lqsj: '2019-12-09',
    zh: '333333333',
    khm: '徐凤年',
    khyh: '建设银行'
  },
  {
    key: 6,
    shzt: '已审核',
    zfzt: '支付成功',
    slr: '徐凤年',
    dlrs: '2',
    slje: 2000,
    lxdh: '13900890012',
    sqsj: '2019-12-12: 12:20',
    hbh: 'SC8999',
    hbrq: '2019-12-09',
    lqfs: '微信',
    lqsj: '2019-12-09',
    zh: '333333333',
    khm: '徐凤年',
    khyh: '建设银行'
  },
  {
    key: 7,
    shzt: '已审核',
    zfzt: '支付成功',
    slr: '徐凤年',
    dlrs: '2',
    slje: 2000,
    lxdh: '13900890012',
    sqsj: '2019-12-12: 12:20',
    hbh: 'SC8999',
    hbrq: '2019-12-09',
    lqfs: '微信',
    lqsj: '2019-12-09',
    zh: '333333333',
    khm: '徐凤年',
    khyh: '建设银行'
  },
  {
    key: 8,
    shzt:'已审核',
    zfzt: '支付成功',
    slr: '徐凤年',
    dlrs: '2',
    slje: 2000,
    lxdh: '13900890012',
    sqsj: '2019-12-12: 12:20',
    hbh: 'SC8999',
    hbrq: '2019-12-09',
    lqfs: '微信',
    lqsj: '2019-12-09',
    zh: '333333333',
    khm: '徐凤年',
    khyh: '建设银行'
  },
  {
    key: 9,
    shzt: '已审核',
    zfzt: '支付成功',
    slr: '徐凤年',
    dlrs: '2',
    slje: 2000,
    lxdh: '13900890012',
    sqsj: '2019-12-12: 12:20',
    hbh: 'SC8999',
    hbrq: '2019-12-09',
    lqfs: '微信',
    lqsj: '2019-12-09',
    zh: '333333333',
    khm: '徐凤年',
    khyh: '建设银行'
  },
  {
    key: 10,
    shzt: '已审核',
    zfzt: '支付成功',
    slr: '徐凤年',
    dlrs: '2',
    slje: 2000,
    lxdh: '13900890012',
    sqsj: '2019-12-12: 12:20',
    hbh: 'SC8999',
    hbrq: '2019-12-09',
    lqfs: '微信',
    lqsj: '2019-12-09',
    zh: '333333333',
    khm: '徐凤年',
    khyh: '建设银行'
  },
  {
    key: 11,
    shzt: '已审核',
    zfzt: '支付成功',
    slr: '徐凤年',
    dlrs: '2',
    slje: 2000,
    lxdh: '13900890012',
    sqsj: '2019-12-12: 12:20',
    hbh: 'SC8999',
    hbrq: '2019-12-09',
    lqfs: '微信',
    lqsj: '2019-12-09',
    zh: '333333333',
    khm: '徐凤年',
    khyh: '建设银行'
  },
  {
    key: 12,
    shzt: '已审核',
    zfzt: '支付成功',
    slr: '徐凤年',
    dlrs: '2',
    slje: 2000,
    lxdh: '13900890012',
    sqsj: '2019-12-12: 12:20',
    hbh: 'SC8999',
    hbrq: '2019-12-09',
    lqfs: '微信',
    lqsj: '2019-12-09',
    zh: '333333333',
    khm: '徐凤年',
    khyh: '建设银行'
  }
];

// for (let i = 0; i < 10; i += 1) {
//   tableListDataSource.push({
//     key: i,
//     disabled: i % 6 === 0,
//     href: 'https://ant.design',
//     avatar: [
//       'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
//       'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
//     ][i % 2],
//     name: `TradeCode ${i}`,
//     title: `一个任务名称 ${i}`,
//     owner: '曲丽丽',
//     desc: '这是一段描述',
//     callNo: Math.floor(Math.random() * 1000),
//     status: Math.floor(Math.random() * 10) % 4,
//     updatedAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
//     createdAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
//     progress: Math.ceil(Math.random() * 100),
//   });
// }

function getRule(req, res, u) {
  let url = u;

  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    // eslint-disable-next-line prefer-destructuring
    url = req.url;
  }

  const params = parse(url, true).query;
  let dataSource = tableListDataSource;

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }

      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        dataSource.filter(item => {
          if (parseInt(`${item.status}`, 10) === parseInt(s.split('')[0], 10)) {
            return true;
          }

          return false;
        }),
      );
    });
    dataSource = filterDataSource;
  }

  if (params.name) {
    dataSource = dataSource.filter(data => data.name.includes(params.name || ''));
  }

  let pageSize = 10;

  if (params.pageSize) {
    pageSize = parseInt(`${params.pageSize}`, 0);
  }

  const result = {
    data: dataSource,
    total: dataSource.length,
    success: true,
    pageSize,
    current: parseInt(`${params.currentPage}`, 10) || 1,
  };
  return res.json(result);
}

function postRule(req, res, u, b) {
  let url = u;

  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    // eslint-disable-next-line prefer-destructuring
    url = req.url;
  }

  const body = (b && b.body) || req.body;
  const { method, name, desc, key } = body;

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      tableListDataSource = tableListDataSource.filter(item => key.indexOf(item.key) === -1);
      break;

    case 'post':
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        href: 'https://ant.design',
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
        name: `TradeCode ${i}`,
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
        desc,
        callNo: Math.floor(Math.random() * 1000),
        status: Math.floor(Math.random() * 10) % 2,
        updatedAt: new Date(),
        createdAt: new Date(),
        progress: Math.ceil(Math.random() * 100),
      });
      break;

    case 'update':
      tableListDataSource = tableListDataSource.map(item => {
        if (item.key === key) {
          return { ...item, desc, name };
        }

        return item;
      });
      break;

    default:
      break;
  }

  const result = {
    list: tableListDataSource,
    pagination: {
      total: tableListDataSource.length,
    },
  };
  return res.json(result);
}

export default {
  'GET /api/compensation/substitute': getRule,
  'POST /api/compensation/rule': postRule,
};
